function gd$(fctId){
        return document.getElementById(fctId);
}

function enabledbackround(sEnabled)
{
	if(sEnabled=="0"){
		$("#txReturnDate").removeClass("txdatec").addClass("txdisabled").attr("disabled",true).val("");
	}
	if(sEnabled=="1"){
		$("#txReturnDate").removeClass("txdisabled").addClass("txdatec").attr("disabled",false);
	}
	
}
$(document).ready(function () {		
	$('#txDepartDate').datepick({minDate: 0,monthsToShow:2});
	$('#txReturnDate').datepick({minDate: 0,monthsToShow:2});
	
	try{
		init_selairport();
		initvalidationinput();
	}catch(e)
	{
		
	}
});

function initvalidationinput()
{
	$.formValidator.initConfig({formid:"fmflight",onerror:function(msg){alert(msg)}});
	$("#txFromCityCode").formValidator().inputValidator({min:3,onerror:"Please provide Departure City"});
	$("#txDestCityCode").formValidator().inputValidator({min:3,onerror:"Please provide Arrival City"});
	$("#txDepartDate").formValidator();
}

function bindpassvalidationinput(obj,smsg)
{
	//$(obj).formValidator().inputValidator({min:3,onerror:smsg});
}
//预订页面校验函数
function initvalidationbook()
{
	$("#txLinkMan").formValidator({empty:false,onshow:"Please provide name",onfocus:"eg. zhangsan",oncorrect:""}).inputValidator({min:3,onerror:"Please provide Name;"});	
	$("#txMobilephone1").formValidator({empty:false,onshow:"Please provide mobile",onfocus:"eg. 13800138000",oncorrect:""}).inputValidator({min:11,max:11,onerror:"手机号码必须是11位的,请确认"}).regexValidator({regexp:"mobile",datatype:"enum",onerror:"your mobile is not correct"});
	$("#txMobilephone2").formValidator({empty:true,onshow:"Please provide mobile",onfocus:"eg. 13800138000",oncorrect:"",onempty:""}).inputValidator({min:11,max:11,onerror:"手机号码必须是11位的,请确认"}).regexValidator({regexp:"mobile",datatype:"enum",onerror:"your mobile is not correct"});
	$("#txEmail").formValidator({onshow:"Please provide Email",onfocus:"eg. air@hotmail.com",oncorrect:"Email correct",defaultvalue:""}).inputValidator({min:6,max:100,onerror:"Email is not correct"}).regexValidator({regexp:"email",datatype:"enum",onerror:"your email is not correct"});
}

//根据日期获取航班信息
function ajaxgetFlightinfo(stclassName,flightdate,requestURL){
	var stdata="";
	var stFromCity,stDestCity,stDoubleTrip,stAirLine,stSupplier;
	
	stAirLine	=$("#hdn_airline").val();
	stDoubleTrip=$("#hdn_flighttype").val();
	
	if (stclassName=="go"){
		stFromCity=$("#hdn_FromCityCode").val();
		stDestCity=$("#hdn_DestCityCode").val();
		$("#hdn_LeaveDate").val(flightdate);
		$("#txLeaveDate1").val(flightdate);
		
		if ($("#txDoubleTrip")=="1"){
			$("#txReturnDate").val(flightdate);
		}
	}else{
		stFromCity=$("#hdn_DestCityCode").val();
		stDestCity=$("#hdn_FromCityCode").val();
		$("#hdn_ReturnDate").val(flightdate);
		$("#txReturnDate").val(flightdate);
	}
	dtLeaveDate	=$("#hdn_DepartDate").val();
	dtReturnDate	=$("#txReturnDate").val();
	var stFlightTime="",stClass="";
	
	stFlightTime		=$("#hdnLeaveTime").val();
	
	stClass="";
	$(":checked[name=seattype]").each(function(){
		stClass+=this.value;	
	});
	stClass+="|"+$("#hdn_goClassSeat").val()
	
	var goflight=$("#txgoFlightData").val();
	goflight=URLencode(goflight);
	
	stdata="&Airline="+stAirLine+"&className="+stclassName+"&FromCity="+stFromCity+"&DestCity="+stDestCity+"&ReturnDate="+dtReturnDate+"&LeaveDate="+dtLeaveDate+"&DoubleTrip="+stDoubleTrip;
	stdata+="&isback="+$("#txisback").val()+"&goflightdata="+goflight;
	//alert(stdata);
	insertFlash("dvmainflight", _baseroot+"images/loading.swf", 310, 100);
	
	$.ajax({
		url:_baseroot+requestURL,
		data:stdata,
		type:"get",cache: false,
		success:function(xml){
			$("#dvmainflight").html(xml);
		}
	});	
}
function URLencode(sStr) 
{
    return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}

function insertFlash(elm, url, w, h) {
	if (!document.getElementById(elm)) return;
	var str = '';
	str += '<object width="'+ w +'" height="'+ h +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0">';
	str += '<param name="movie" value="'+ url +'">';
	str += '<param name="wmode" value="opaque">';
	str += '<param name="quality" value="autohigh">';
	str += '<embed width="'+ w +'" height="'+ h +'" src="'+ url +'" quality="autohigh" wmode="opaque" type="application/x-shockwave-flash" plugspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
	str += '</object>';
	document.getElementById(elm).innerHTML ="<table width=100% height=300 ><tr><td align=center>" +str+"</td></tr></table>";
}

function show_moreclass(flightNumber)
{
	for(var i=0;i<20;i++){
		$("#trF_"+flightNumber+"_"+i).toggle();
	}
	return false;
}
//预订航班
function bookthisflight(obj)
{
	
	//设置去程信息，回程信息
	//回程的设置回程信息
	
	if($("#hdn_flighttype").val()=="2" && $("#hdn_isback").val()=="1"){
		$("#hdn_backFlight").val(obj.getAttribute("data"));
	}else{
		//默认去程信息
		$("#hdn_goFlight").val(obj.getAttribute("data"));	
	}
	
	gd$("fmbook").target="_self";
	gd$("fmbook").submit();	
	
	return false;
}
/************预订函数相关JS*********************************************************/
//删除当前行
function deletethisrow(obj){
	$(obj.parentNode.parentNode).remove();
	return false;
}
//增加模板行
function addmodelbyIndex(index,iMaxI){
	var objtb	=gd$("tbPassenger_"+index);
	var objtbM=gd$("tbModel_"+index);
	var newtr=objtb.insertRow(-1);
	var rowlength=objtb.rows[0].cells.length;
	newtr.align="center";
	newtr.height="32px";
	for(var i=0;i<rowlength;i++){
		var td=newtr.insertCell(-1);		
		var html=objtbM.rows[0].cells[i].innerHTML;
		html=html.replace(/REPLACE_INDEX/g,iMaxI);
		td.innerHTML=html;
	}
}
//增加机票的人数
function addairTicketPassenger(iMaxI){
	var maxP=5;
	
	if($("#hdn_maxseat").length>0){
		maxP=parseInt($("#hdn_maxseat").val());
	}
	
	if(document.getElementsByName("PassengerType[]").length==maxP+1){
		alert("加人不能超过"+maxP+"个");
		return false;
	}
	addmodelbyIndex(1,iMaxI);
		jValue={
			"Serrial":iMaxI,
			"PassengerName":"",
			"PassengerType":"0",
			"CTType":"0",
			"CTID":"",
			"Note":"",
			"PTel":""
		};
 	setAirTicketValue(iMaxI,jValue);
 	gd$("hdn_totalAdult").value=document.getElementsByName("PassengerType[]").length-1;
 	//selectAirpolicy(null,-1);
 	return false;
}

//删除客人数
function delairTicketPassenger(obj){
	if(document.getElementsByName("PassengerType[]").length==2){
		alert(" at least a guest ");
		return false;
	}else{
		deletethisrow(obj);
		gd$("hdn_totalAdult").value=document.getElementsByName("PassengerType[]").length-1;		
	}
	caltotalpassenger();
	return false;	
}

function setAirTicketValue(index,jValue){
	//bindpassvalidationinput(gd$("CTID_"+index),"Please provide ID Number");
	
	gd$("Serrial_"+index).value=jValue.Serrial;
	//gd$("PassengerName_"+index).value=jValue.PassengerName;
	gd$("PassengerType_"+index).value=jValue.PassengerType;
	
	if (jValue.PassengerType=="0"){
		$("#spPassengerType_"+index).html("Adult")
	}else{
		$("#spPassengerType_"+index).html("Child")
	}
	
	
	gd$("CTID_"+index).value=jValue.CTID;
	gd$("sel_CTType_"+index).value=jValue.CTType;	
	
	//gd$("Note_"+index).value=jValue.Note;
	
	gd$("PassengerTel_"+index).value=jValue.PTel;
	
	caltotalpassenger();
}

//去掉空格
function Trim(str){
	return str.replace(/(^[\s]*)|([\s]*$)/g, "");
}
//校验乘客信息
function verifySinglePassName(psgname){
	var pname = Trim(psgname.value);
	var acceptname_cn = /^([\u4E00-\u9FA5]{1,}[a-zA-Z]{0,}$){1,10}/;
	var acceptname_en = /^([a-zA-Z\/\s]{2,}\/{1}[a-zA-Z\/\s]{2,}){1,20}$/;
	var enstr = /^[a-zA-Z]/;
	var cnstr = /^[\u4E00-\u9FA5]/;
	var spstr = /\s/g;
	
	if(!isNaN(pname))
	{	 
	alert("Please enter a valid Chinese name, such as: zhang SAN YANG");  
	psgname.focus()
	return false;
	}
	
	if(pname == ""){
		alert("Name not allow for empty");
		return false;
	}
	
	//检查是否有非GB2312字符
	//if(!CheckGB2312(pname)){
	//	 alert("旅客姓名“"+ pname +"”您的姓名中包含生僻字，请将该生僻字改为拼音输入。如果你用的繁体字，请将繁体字转换成简体字。");
	//	 return false;
	//}
	
	if(enstr.test(pname)){
		
		if(pname.length>26){
			//alert("英文姓名长度最多为20位！");
		  alert("Most English full name for 26 bits length");
		  psgname.focus()
			return false;
		}
		if(!acceptname_en.test(pname)){
			//alert("请输入有效的英文姓名，如：Tom/Jackson");
			alert("Please enter a valid English name, such as: Tom/Jackson");
			psgname.focus()
			return false;
		}
	}		
	if(cnstr.test(pname)) {
		if(name.length > 20){
			//alert("中文姓名长度最多为20位！");
			alert("Chinese name length up to the 13th");
			psgname.focus()
			return false;
		}
		if(!acceptname_cn.test(pname)){
			//alert("请输入有效的中文姓名，如：张三");
	 			alert("Please enter a valid Chinese name, such as: zhang SAN YANG ");
	 			psgname.focus()
			return false;
		}	
	}
	
	
	return true;
}

//确认机票订单
function confirmairTicket(){
	var checkpassenger=false;
		
	//检查乘客姓名是否符号要求
	$("input[name='PassengerName[]']:not(:last)").each(function(){
		
		if(this.id!="PassengerName_REPLACE_INDEX" && this.type=="text"){
			if(verifySinglePassName(this)){
				checkpassenger=true;
			}else{
				checkpassenger=false;
				return false;	
			}
		}else{
			checkpassenger=true;	
		}
	});

	if(!checkpassenger){
		return false;	
	}
	
	//全角转换半角  fullChar2halfChar
	$("input[name=CTID]:not(:last)").each(function(){
		this.value=fullChar2halfChar(this.value);
	});
	
	if(!checkpassenger){
		return false;	
	}
	
	if(CheckFormSubmit(gd$("formbook"))){
		if($("#chkAgreement").attr("checked")==false)
		{
			alert("Please read terms and conditions for international flight reservation, and select \"I accept\" to continue your reservation")
			return false;	
		}
		//+gd$("spbooknote").innerText
		if(confirm("Click [confirm] create order,Click [cancel] cancel the order")){
			//gd$("formbook").target="_book";
			gd$("formbook").submit();
			gd$("btn_create").disabled=true;
		}
		
	}
}

//全角转换半角
function fullChar2halfChar(str)
{
	var result = '';
	for (i=0 ; i<str.length; i++)
	{
		code = str.charCodeAt(i);             //获取当前字符的unicode编码
		if (code >= 65281 && code <= 65373)   //unicode编码范围是所有的英文字母以及各种字符
		{
			result += String.fromCharCode(str.charCodeAt(i) - 65248);    //把全角字符的unicode编码转换为对应半角字符的unicode码
		}
		else if (code == 12288)                                      //空格
		{
			result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);//半角空格
		}else
		{
			result += str.charAt(i);                                     //原字符返回
		}
	}
	return result;
}
//设置人数
function setpassengernum(obj)
{
	var icount=document.getElementsByName("PassengerName[]").length-1;
	var ipasnum=parseFloat(obj.value)+1;
	
	for(var i=icount;i<ipasnum;i++){
		addmodelbyIndex(1,i);
		jValue={
			"Serrial":iMaxI++,
			"PassengerName":"",
			"PassengerType":"0",
			"CTType":"0",
			"CTID":"",
			"Note":"",
			"PTel":""
		};
			
		setAirTicketValue(i,jValue);
	}
	var irows=gd$("tbPassenger_1").rows.length;
	for(var i=icount-ipasnum;i>0;i--){
		var obj=gd$("tbPassenger_1").rows[gd$("tbPassenger_1").rows.length-1];
		$(obj).remove();
	}
	
	caltotalpassenger();
}
//计算总价格
function caltotalpassenger()
{
	var iPassenger=document.getElementsByName("Serrial[]").length-1;
	var ieachprice=gd$("hdn_eachprice").value;
	$("#spTotalprice").html(ieachprice*iPassenger);
	$("#spPassenger").html(iPassenger);
	
}

function showTGQNote(airline,classcode)
{
	$.ajax({
		url:_baseroot+"home/getTGQNote",
		data:"airline="+airline+"&classcode="+classcode,
		success:function(xml){
			alert(xml);	
		}
	});
	return false;
}
//显示回程信息
function showbackAirTicket(obj)
{
	//设置去程信息，回程信息
	var stData="",arData;
	stData=obj.getAttribute("data");
	//arData=stData.split("|");
	
	$("#hdn_airline").val(obj.getAttribute("Airline"));
	
	$("#txgoFlightData").val(stData);
	$("#txisback").val(1);
	gd$("fmflight").target="_self";
	gd$("fmflight").submit();	
	
	return false;
}

function resetgoFlight(iMark)
{
	
	$("#txgoFlightData").val("");
	$("#txisback").val(0);
	
	if(iMark==1){
		gd$("fmflight").target="_self";
		gd$("fmflight").submit();
	}
	return false;
}

function clearTxt(obj,tip) {
	
  if (obj.value == tip)
   	obj.value="" ;	
   $(obj).css({color: "#000",fontSize:"13pt"});	   	
  return ;
}
function fillTxt(obj,tip) {
  if (obj.value == "" ){
  	$(obj).val(tip).css({color: "gray",fontSize:"10pt"});
  }
  return ;
}